<!-- create begin -->
<div class="create">
   
    <!-- 表单 -->   
    <section class="panel-body" >
        <form role="form" class="form-create">

            <div class="form-group col-sm-12">
                <span>编号</span>
                <input type="text" class="form-control" placeholder="编号" name="sn" v-model="param.sn">
            </div>

            <div class="form-group col-sm-6">
                <span>销售人</span>
                <input type="text" class="form-control" placeholder="销售人" name="author" v-model="param.author">
            </div>

            <div class="form-group col-sm-6">
                <span>客户</span>
                <select name="customer" class="form-control comboSelect" v-model="param.customer">
                    {volist name="customer" id="vo"}
                        <option value="{$vo.id}" >{$vo.title}</option>
                    {/volist}
                </select>
            </div>

            <div class="form-group col-sm-6">
                <span>销售渠道</span>
                <select name="channel" class="form-control comboSelect" v-model="param.channel">
                    <option value="默认">默认</option>
                    <option :value="item" v-for="item in channel" v-text="item"></option>
                </select>
            </div>

            <div class="form-group col-sm-6">
                <span>销售日期</span>
                <input type="text" class="form-control" placeholder="销售日期" name="date" v-model="param.sale_date" id="sale_date">
            </div>

            {include file="common/select-table" /}

            <div class="form-group col-sm-12">
                <button type="button" class="btn btn-sm btn btn-primary" @click="save"> 保 存 </button>
                <button type="reset" class="btn btn-sm btn-success ">  重 置 </button> 
            </div>

        </form>
    </section>

</div>
<!-- create end-->


<script>
 var list = [];
 var layer_index = 0;

// seajs
seajs.use(['vendor/comboSelect/jquery.combo.select','vendor/channel','vendor/laydate/laydate'], function() {

        var data = {
            name: '名称',
            model: '型号',
            spec: '规格',
            color: '颜色',
            num: '数量',
            unit: '单位',
            price: '单价',
            count: '合计',
        };

    // vue
    var create = new Vue({
        el: '.create',
        data: {
            data: data,
            list: list,
            count:0,
            channel:channel,
            param:{
                sn:"{:date('Ymdhis').rand(100,999)}",
                author:"{$admin_user.nickname}",
                channel: this.channel[0],
                sale_date: "{:date('Y-m-d',time() )}",
                customer: "{$customer[0]['id']}",
                data: this.list,
                count: this.count
            }
        },
        mounted: function (){
            $('.comboSelect').comboSelect();
            laydate.render({
                elem: '#sale_date',
                theme: '#2DCB73'
            });
        },
        methods: {
            save: function () {
                this.param.data = this.list;
                this.param.count = this.count;

                $.ajax({
                    url: "{:url('save')}",
                    method: 'POST',
                    dataType: 'json',
                    data: this.param,
                    success: this.callback
                });
            },
            callback:function(data){
                if( data.error >0 ){
                    toastr.error(data.msg);
                }else{
                    toastr.success(data.msg);
                    Table.methods.refresh();
                    layer.closeAll('page');
                }
            },
            addLine:function(){
                $.get("{:url('product/select')}", function(data) {
                    layer_index = layer.open({
                        type: 1,
                    　　title: "选择产品",
                    　　shadeClose: true,
                    　　shade: 0.4,
                        anim: 5,
                        area: ['100%','500px'],
                    　　content: data
                    });
                });

            },
            delLine:function(index){
                this.list.splice(index,1);
                // delete this.list[index];
            },
            changeSum:function(index){
                this.list[index].count = this.list[index].num * this.list[index].price;
                this.SumCount();
            },
            SumCount:function(){
                this.count = 0;
                for(var index in this.list) {  
                    this.count += parseFloat( this.list[index].count );
                };  
            }

        },
        watch: {
            list:function(val,old){
                this.SumCount();
            }
        },


    });

        
});

</script>